/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.supermercadobarateiro.bd;

import br.edu.ifal.supermercadobarateiro.modelo.Funcionario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Mikaely Anaeve
 */
public class BdFuncionario {

    public void insere(Funcionario f) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();

        String sql = "insert into cadastrofuncionario "
                + "(nome,cidade,email,senha) "
                + "values (?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, f.getNome());
        
        stmt.setString(2, f.getCidade());
        stmt.setString(3, f.getEmail());
        
        // stmt.setString(7, f.getSexo());
        
       stmt.setString(4, f.getSenha());
//        stmt.setDate(10, new java.sql.Date(f.getDatadeNascimento().getTimeInMillis()));
        stmt.execute();
        stmt.close();
        con.close();
    }

    public void altera(Funcionario f) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "update cadastrofuncionario set nome=?"
                + " where cod=?";

        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, f.getNome());
        /*stmt.setInt(2, f.getCpf());
        stmt.setInt(3, f.getTelefone());
        stmt.setString(4, f.getCidade());
        stmt.setString(5, f.getEmail());
       stmt.setInt(6, f.getCep());*/
        stmt.setLong(7 , f.getId());
        //stmt.setDate(10, new java.sql.Date(f.getDatadeNascimento().getTimeInMillis()));

        stmt.execute();
        stmt.close();
        con.close();
    }

    public void remove(Funcionario f) throws SQLException {

        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = " delete from cadastrofuncionario where id = ?";
        PreparedStatement stmt = con.prepareStatement("delete "
                + "from cadastrofuncionario where id=?");
        stmt.setLong(2, f.getId());
        stmt.execute();
        stmt.close();



    }

    public Funcionario busca(int id) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "select *from cadastrofuncionario where cod = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, id);
        ResultSet rs = stmt.executeQuery();
        Funcionario f1 = new Funcionario();
        while (rs.next()) {

            f1.setNome(rs.getString("nome"));
            f1.setEmail(rs.getString("email"));
             f1.setCidade(rs.getString("cidade"));
            f1.setCep(rs.getInt("cep"));
             f1.setCpf(rs.getInt("cpf"));
           
            f1.setTelefone(rs.getInt("telefone"));
                f1.setSenha(rs.getString("senha"));
            


        }
        rs.close();
        stmt.close();
        con.close();
        return f1;
    }
}
